$(function (){
    //删除
    deleteById();
    initPage();
    //点击全选选中全部商品
    $("#quanbu").click(function (){
        //是否选中子类
        $(".son-checked").prop("checked",$(this).prop("checked"));
        //计算总价格
        sumPrice();
    });
    //点击子类选项
    $("#tbody").on("click","tr > td > .son-checked",function (){
        //是否选中父类
        if($(".son-checked:checked").length==$(".son-checked").length){
            $("#quanbu").prop("checked",true);
        }else{
            $("#quanbu").prop("checked",false);
        }
        //计算总价格
        sumPrice();
        //计算总价
        sumPrice();
    });
    //点击提交订单
    $("#goshopping").click(function (){
        if($(".son-checked:checked").length==0){
            alert("请选中购买的商品");
            return false;
        }else{
            //得到选中的商品的下标
            let indexArr=new Array();
            $(".son-checked:checked").each(function (){
                indexArr.push($(this).data("index"));
            });
            //保存下标到session
            let indexObj=new Object();
            indexObj.indexStr=indexArr;
            indexObj=JSON.stringify(indexObj);
            sessionStorage.setItem("indexObj",indexObj);
            //跳转到确认页面
            location.href="/my/order-confirm.html";
        }
    });

});
//页面初始化/查询所有订单
function initDate(){
    //拿到session里面的对象
    var msgUser = sessionStorage.getItem("user");
    //当前用户的id
    var uid = JSON.parse(msgUser).id;

    //调用查询全部方法
    $.get(
        "ProductCarQuery",
        {uid:uid},
        function (msg){
            productCars=msg.data;
            //转成字符串
            var shoppingCar = JSON.stringify(productCars);
            //存在session里面
            sessionStorage.setItem("shoppingCar",shoppingCar);
            var nump=0;
            for (let i = 0; i < productCars.length; i++) {
                nump=nump+productCars[i].num * productCars[i].price;
                console.log(nump);
                let tr=$(`<tr>
                            <td width="30">
                                <input id="chk1" data-toggle="check-sub" type="checkbox" class="son-checked" value="${productCars[i].num * productCars[i].price}" data-index="${i}"/>

                            </td>
                            <td>
                                <label for="chk1" class="text-muted">${productCars[i].name}</label>
                            </td>
                            <td>
                                <label for="chk1">${productCars[i].price}</label>
                            </td>
                            <td>
                                <label for="chk1">${productCars[i].num}</label>
                            </td>
                            <td>
                                <label for="chk1">${productCars[i].price * productCars[i].num}</label>
                            </td>
                            <td width="70">
                                <button data-id="${productCars[i].id}" data-toggle="remove" type="button" class="btn btn-sm py-0 btn-outline-danger deleteId">
                                    <i class="glyphicon-remove"></i>
                                    <span class="d-none d-md-inline">移除</span>
                                </button>
                            </td>
                        </tr>`);
                $("#tbody").append(tr);
            }
            $("#sum1").text(nump);
        },
        "json"
    )
}
//计算总价钱
function sumPrice(){
    var sumPrices=0;
    $(".son-checked:checked").each(function (){
        console.log($(this).val());
        sumPrices=sumPrices+parseFloat($(this).val());

    })

    $("#sum2").text(sumPrices);
}
//点击
function initPage(){
    var path = location.href;
    if(path.indexOf("&count")==-1){
        initDate();
    }else{
        //点击购买进入的购物车
        var pid = path.substring(path.indexOf("pid=")+4,path.indexOf("&"));
        var count = path.substring(path.indexOf("count=")+6);
        var uid = JSON.parse(sessionStorage.getItem("user")).id;

        //修改购物车
        $.post(
            "AddProductCar",
            {id:pid,count:count,uuid:uid},
            function (msg){
               if (msg.data==1){
                   //重新加载数据
                   initDate();
               }else {
                   alert("添加失败");
               }
            },
            "json"
        );
    }
}

//删除
function deleteById() {
    $("#tbody").on("click", "tr > td > .deleteId ",function (){
        var id = $(this).data("id");
        if (confirm("确定要删除吗")){
            $.get(
                "ProductCarByIdDelete",
                {id:id},
                function (msg){
                   if (msg.data==1){
                       //刷新页面
                       location.reload();
                   }else {
                       alert("删除失败，数据有误");
                   }
                },
                "json"
            )
        }
    });
}